package com.example.dispatherview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by Alex-Zhang on 2018/1/17.
 * 内切圆--> 内切矩形按钮
 */

public class RecordView extends View {

    private Paint mPaint;

    private RectF mRect;


    private float minFilletRadius = 10;

    private float defaultFilletRadius;

    private static final float DEFAULT_WIDTH = dp2px(200);

    private static final float DEFAULT_HEIGHT = dp2px(80);

    private float defaultPointXY;

    private float rawPointXY = 20;

    private float innerSquareWidth = 80;

    private float maxPointXY = (float) ((2 - Math.sqrt(2)) / 4 * innerSquareWidth) + rawPointXY;

    private float minInnerSquare = (float) (Math.sqrt(2) / 2 * innerSquareWidth);

    private float maxFilletRadius = innerSquareWidth / 2;

    private float defaultInnerSquareWidth;
    private ValueAnimator valueAnimator;
    private ValueAnimator valueAnimatorPoint;
    private ValueAnimator valueAnimatorRadius;
    private boolean isFinish;

    public RecordView(Context context) {
        super(context);
        init();
    }


    public RecordView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RecordView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLUE);

        defaultFilletRadius = maxFilletRadius;

        defaultInnerSquareWidth = innerSquareWidth;

        defaultPointXY = rawPointXY;

        mRect = new RectF();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        if (widthMode == MeasureSpec.UNSPECIFIED
                || widthMode == MeasureSpec.AT_MOST) {
            widthMeasureSpec = MeasureSpec.makeMeasureSpec((int) DEFAULT_WIDTH, MeasureSpec.EXACTLY);
        }
        if (heightMode == MeasureSpec.UNSPECIFIED
                || heightMode == MeasureSpec.AT_MOST) {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) DEFAULT_HEIGHT, MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mRect.set(defaultPointXY, defaultPointXY, defaultPointXY + defaultInnerSquareWidth, defaultPointXY + defaultInnerSquareWidth);

        canvas.drawRoundRect(mRect, defaultFilletRadius, defaultFilletRadius, mPaint);
    }

    @Override
    public void setOnTouchListener(OnTouchListener l) {
        super.setOnTouchListener(l);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (isFinish) {
                    stopAnimation();
                }else {
                    startAnimation();

                }
                break;
            default: {
                super.onTouchEvent(event);
            }
        }
        return true;
    }

    private static int dp2pxInt(float dp) {
        return (int) dp2px(dp);
    }

    private static float dp2px(float dp) {
        Resources r = Resources.getSystem();
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
    }

    private void startAnimation() {
        valueAnimator = ValueAnimator.ofFloat(maxFilletRadius, minFilletRadius);
        valueAnimator.setDuration(500);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                defaultFilletRadius = (float) animation.getAnimatedValue();
                Log.d("onAnimationUpdate", "defaultFilletRadius: "+defaultFilletRadius);
                invalidate();
            }
        });

        valueAnimatorPoint = ValueAnimator.ofFloat(rawPointXY, maxPointXY);
        valueAnimatorPoint.setDuration(500);
        valueAnimatorPoint.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                defaultPointXY = (float) animation.getAnimatedValue();
                Log.d("onAnimationUpdate", "defaultPointXY: "+defaultPointXY);
                invalidate();
            }
        });

        valueAnimatorRadius = ValueAnimator.ofFloat(innerSquareWidth, minInnerSquare);
        valueAnimatorRadius.setDuration(500);
        valueAnimatorRadius.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                defaultInnerSquareWidth = (float) animation.getAnimatedValue();
                Log.d("onAnimationUpdate", "defaultInnerSquareWidth: "+defaultInnerSquareWidth);
                invalidate();
            }
        });

        valueAnimatorRadius.start();
        valueAnimatorPoint.start();
        valueAnimator.start();
        isFinish = true;
    }

    private void stopAnimation(){
        valueAnimatorRadius.reverse();
        valueAnimatorPoint.reverse();
        valueAnimator.reverse();
        isFinish =false;
    }


}
